using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace JingQiao.Ads.Infrastructure.Data.EF.DomainObject
{
    using Domain.Model.DomainObject;

    public partial class Template : ITemplate
    {
        #region Primitive Properties
    
        public virtual int TemplateId
        {
            get;
            set;
        }
    
        public virtual string Name
        {
            get;
            set;
        }
    
        public virtual string Content
        {
            get;
            set;
        }
    
        public virtual string Summary
        {
            get;
            set;
        }
    
        public virtual System.DateTime CreateDate
        {
            get;
            set;
        }
    
        public virtual string Creator
        {
            get;
            set;
        }


        public virtual ICollection<IPosition> Position
        {
            get;
            set;
        }
        private ICollection<IPosition> _position;

        #endregion

        #region Navigation Properties

        public virtual ICollection<Position> OwnedPositions
        {
            get
            {
                if (_ownedPositions == null)
                {
                    var newCollection = new FixupCollection<Position>();
                    newCollection.CollectionChanged += FixupOwnedPositions;
                    _ownedPositions = newCollection;
                }
                return _ownedPositions;
            }
            set
            {
                if (!ReferenceEquals(_ownedPositions, value))
                {
                    var previousValue = _ownedPositions as FixupCollection<Position>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupOwnedPositions;
                    }
                    _ownedPositions = value;
                    var newValue = value as FixupCollection<Position>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupOwnedPositions;
                    }
                }
            }
        }
        private ICollection<Position> _ownedPositions;

        #endregion

        #region Association Fixup

        private void FixupOwnedPositions(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (Position item in e.NewItems)
                {
                    item.BelongToTemplate = this;
                }
            }

            if (e.OldItems != null)
            {
                foreach (Position item in e.OldItems)
                {
                    if (ReferenceEquals(item.BelongToTemplate, this))
                    {
                        item.BelongToTemplate = null;
                    }
                }
            }
        }

        #endregion
    }
}
